ifeq ($(OS), Windows_NT)
  PIC_FLAG =
  LIBS = -lws2_32
  EXE = .exe
  STATIC_EXT = .lib
  DYNAMIC_EXT = .dll
else
  PIC_FLAG = -fPIC
  LIBS = -lm -ldl -lpthread
  EXE =
  STATIC_EXT = .a
  DYNAMIC_EXT = .so
endif

CXXFLAGS += -D_GNU_SOURCE -D_LARGEFILE_SOURCE

HEADERS = arith.hpp array.hpp builtins.hpp bvector.hpp bytecode.hpp   \
        config.hpp cons.hpp coro.hpp custom.hpp defs.hpp event.hpp   \
        floatp.hpp function.hpp llio.hpp initop.hpp integer.hpp interp.hpp   \
        io.hpp memory.hpp dynlib.hpp khipu.hpp stream.hpp str.hpp   \
        symbol.hpp table.hpp thread.hpp tuple.hpp xtime.hpp types.hpp   \
        sysdeps/atomic.hpp utils/chmask.hpp utils/raw_acc.hpp   \
        utils/sorted_list.hpp

OBJS = arith.o integer.o floatp.o memory.o stream.o str.o   \
       dynlib.o bvector.o table.o array.o cons.o thread.o interp.o misc.o   \
       symbol.o tuple.o io.o builtins.o function.o coro.o llio.o   \
       bytecode.o event.o initop.o types.o custom.o compiler.o eval.o

LOBJS = $(OBJS:.o=.lo)
UOBJS = $(LOBJS)
STATIC_LIB = libkhipu$(STATIC_EXT)
DYNAMIC_LIB = libkhipu$(DYNAMIC_EXT)
KPLIB = $(DYNAMIC_LIB)

-include config.mak

AR = $(CROSS_COMPILE)ar
RANLIB = $(CROSS_COMPILE)ranlib
CXXFLAGS += $(CXXFLAGS_AUTO)

all: khipu$(EXE)

khipu$(EXE): $(KPLIB) khipu.cpp
	$(CXX) $(CXXFLAGS) khipu.cpp -o khipu$(EXE) $(LDFLAGS) $(KPLIB) $(LIBS)

%.o: %.cpp $(HEADERS)
	$(CXX) $(CXXFLAGS) -c $< -o $@

%.lo: %.cpp $(HEADERS)
	$(CXX) $(CXXFLAGS) $(PIC_FLAG) -c $< -o $@

$(DYNAMIC_LIB): $(LOBJS) $(HEADERS)
	$(CXX) $(CXXFLAGS) $(PIC_FLAG) -shared -o $@ $(LOBJS)

$(STATIC_LIB): $(UOBJS) $(HEADERS)
	rm -f $@
	$(AR) rc $@ $(OBJS)
	$(RANLIB) $@

clean:
	rm -f *.o *.lo khipu$(EXE) libkhipu*

